<?php
/**
 * author: jintao
 */
class apibusiness_response_order_mengdian_b2cv1 extends  apibusiness_response_order_mengdian_abstract{
    /**
     * 是否接收(除活动订单外的其他订单)
     *
     * @return void
     * @author
     **/
    protected function accept_dead_order(){
        $rs = parent::accept_dead_order();
        #订单取消的，先放过
        if ($rs == false && $this->_ordersdf['status'] == 'dead') {
            unset($this->_apiLog['info']['msg']);
            return true;
        }
        return $rs;
    }
    /*
     * 是否接收订单
    * @return void
    * @author
    **/
    protected function canAccept(){
        $result = parent::canAccept();
        if ($result === false) {
            return false;
        }
        #未支付的订单拒收
        if ($this->_ordersdf['pay_status'] == '0') {
            $this->_apiLog['info']['msg'] = '未支付订单不接收';
            return false;
        }
        return true;
    }

    /**
     * @return void
     * @author
     **/
    public function canCreate(){
        $result =  parent::canCreate();
        if($result === false){
            return false;
        }
        if ($this->_ordersdf['status'] != 'active') {
            $this->_apiLog['info']['msg'] = ($this->_ordersdf['status'] == 'dead') ? '取消的订单不接收' : '完成的订单不接收';
            return false;
        }
        #萌店创建订单的时候，未支付订单不接受
        if($this->_ordersdf['pay_status'] != '1'){
            $this->_apiLog['info']['msg'] =  '未支付萌店订单不接收';
            return false;
        }
        return true;
    }

    /**
     * 允许更新
     *
     * @return void
     * @author
     **/
    protected function canUpdate()
    {

        $this->canCancelOrder = false;//是否能取消订单

        if( ($this->_tgOrder['ship_status'] == 0) && ($this->_tgOrder['status'] == 'active') && ($this->_ordersdf['status'] == 'dead') && ($this->_ordersdf['pay_status'] == '5') ){
            $this->canCancelOrder = true;
        }elseif( ($this->_ordersdf['shipping']['is_cod'] == 'true') && ($this->_ordersdf['status'] != 'active') ){
            $this->_apiLog['info']['msg'] = '取消的订单不接收';
            return false;
        }

        return parent::canUpdate();
    }
    #更新订单前的操作
    public  function preUpdate(){
        parent::preUpdate();
        if($this->_ordersdf['status'] == 'dead'){
            #1、未发货的订单，整单更新为退款中
            if (($this->_tgOrder['status'] == 'active') &&($this->_tgOrder['ship_status'] == '0')) {
                if($this->canCancelOrder == true){
                    $this->_newOrder['archive'] = '1';
                    $this->_newOrder['process_status'] = 'cancel';
                    $this->_newOrder['status'] = 'dead';
                    $this->_newOrder['confirm'] = 'Y';
                }
            }
            #2、本地已退款或已完成
            else {
                $this->_apiLog['info'][] = '返回值:已完成订单不更新!';
                return false;
            }
        }
    }

    /**
     * 更新完成后操作
     *
     * @return void
     * @author
     **/
    protected function postUpdate()
    {
        parent::postUpdate();

        if($this->canCancelOrder == true){
            $memo = '由于前端店铺:'.$this->_shop['name'].'订单全额退款，系统自动取消';
            $orderModel = app::get(self::_APP_NAME)->model('orders');
            $rs = $orderModel->cancel($this->_newOrder['order_id'],$memo,true,'async');
            $this->_apiLog['info'][] = $memo;
        }

    }
}